package com.rattsoftware.client.views;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.CheckBoxGroup;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.rattsoftware.client.AppMessages;
import com.rattsoftware.client.events.AppEvents;
import com.rattsoftware.client.model.DepartmentSummary;
import com.rattsoftware.client.model.StaffSummary;
import com.rattsoftware.client.model.TestData;
import com.rattsoftware.client.util.ProgressIndicator;
import com.rattsoftware.server.admin.model.Department;
import com.rattsoftware.server.admin.model.RattsModelConstants;

public class NewEditStaffView extends View {
	final AppMessages appMessages = GWT.create(AppMessages.class);

	private CheckBoxGroup staffTitle;
	private CheckBox titleMr;
	private CheckBox titleMrs;
	private CheckBox titleMiss;
	private CheckBox titleDoctor;
	private CheckBox titleProffesor;
	private CheckBox titleAssociateProffesor;
	private CheckBox titleSeniorLecturer;
	private CheckBox titleLecturer;
	private CheckBox titleJuniorLecturer;
	private CheckBox titleTeachingAssistant;
	
	private TextField<String> firstName;
	private TextField<String> lastName;
	private TextField<String> phoneNumber;
	private TextField<String> emailAddress;
	private TextField<String> residentialAddress;
	private ComboBox<DepartmentSummary> departmentNames;
	private DateField dateOfBirth;
	
	// Employment Type
	private RadioGroup employmentType;
	private Radio radioPermanent;
	private Radio radioTemporory;
	private Radio radioContract;
	
	// Employment Type
	private RadioGroup staffType;
	private Radio radioFullTime;
	private Radio radioPartTime;
	
	// Employment Type
	private RadioGroup gender;
	private Radio radioMale;
	private Radio radioFemale;
	
	private StaffSummary staffSummary;
	
	// need a department service, Course unit service, Course Service,
	// Staff service
	
	
	private Window window;

	public NewEditStaffView(Controller controller) {
		super(controller);
	}

	@Override
	protected void handleEvent(AppEvent event) {
		GWT.log("NewEditStaffView : handleEvent");
		if (event.getType() == AppEvents.NewStaff) {
			createUI("Add New Staff");
		} else if (event.getType() == AppEvents.EditStaff) {
			staffSummary = event.getData();
			createUI("Edit Staff Details for " + staffSummary.getName());
		}
	}

	private void createUI(String title) {
		window = new Window();
		window.setHeading(title);
		window.setModal(true);
		window.setPlain(true);
		window.setClosable(false);
		window.setMaximizable(false);
		window.setDraggable(true);
		window.setResizable(false);
		window.setSize(760, 495);
		window.setScrollMode(Scroll.AUTOY);
		window.setLayout(new FitLayout());

		ProgressIndicator.showProgressBar();
		window.add(getStaffPanel());
		ProgressIndicator.hideProgressBar();
		window.show();
	}

	public ListStore<DepartmentSummary> getDepartmentStore() {
		List<Department> departments = TestData.getDepartments();
		Collections.sort(departments, new Comparator<Department>() {
			public int compare(Department arg0, Department arg1) {
				return arg0.getName().compareTo(
						arg1.getName());
			}
		});

		ListStore<DepartmentSummary> deptStore = new ListStore<DepartmentSummary>();

		for (Department department : departments) {
			deptStore.add(new DepartmentSummary(department));
		}
		return deptStore;
	}

	@SuppressWarnings("unchecked")
	private FormPanel getStaffPanel() {
		FormData formData = new FormData("100%");

		FormPanel panel = new FormPanel();
		panel.setFrame(true);
		panel.setHeaderVisible(false);
		panel.setSize(700, -1);
		panel.setLabelAlign(LabelAlign.TOP);

		LayoutContainer main = new LayoutContainer();
		main.setLayout(new ColumnLayout());

		LayoutContainer left = new LayoutContainer();
		left.setStyleAttribute("paddingRight", "10px");
		FormLayout layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		left.setLayout(layout);

		LayoutContainer right = new LayoutContainer();
		right.setStyleAttribute("paddingLeft", "10px");
		layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		right.setLayout(layout);
		
		LayoutContainer titleContainer = new LayoutContainer();
		layout = new FormLayout();
		layout.setLabelAlign(LabelAlign.TOP);
		titleContainer.setLayout(layout);
		
		// course types
		titleMr = new CheckBox();
		titleMr.setBoxLabel("Mr");
		titleMr.addListener(Events.Change, new Listener<FieldEvent>() {
            public void handleEvent(FieldEvent fe) {
            	if (titleMr.getValue()) {
            		titleMrs.setValue(false);
    				titleMiss.setValue(false);
            	}
            }
		});
		
		titleMrs = new CheckBox();
		titleMrs.setBoxLabel("Mrs");
		titleMrs.addListener(Events.Change, new Listener<FieldEvent>() {
			@Override
			public void handleEvent(FieldEvent be) {
				if (titleMrs.getValue()) {
					titleMr.setValue(false);
					titleMiss.setValue(false);
				}
			}
		});
		
		titleMiss = new CheckBox();
		titleMiss.setBoxLabel("Miss");
		titleMiss.addListener(Events.OnClick, new Listener<FieldEvent>() {
			@Override
			public void handleEvent(FieldEvent be) {
				if (titleMiss.getValue()) {
					titleMr.setValue(false);
					titleMrs.setValue(false);
				}
			}
		});
		
		titleDoctor = new CheckBox();
		titleDoctor.setBoxLabel("Doc.");
		titleDoctor.addListener(Events.OnClick, new Listener<FieldEvent>() {
			@Override
			public void handleEvent(FieldEvent be) {
				if (titleDoctor.getValue()) {
					titleProffesor.setValue(false);
					titleAssociateProffesor.setValue(false);
				}
			}
		});
		
		titleProffesor = new CheckBox();
		titleProffesor.setBoxLabel("Prof.");
		titleProffesor.addListener(Events.OnClick, new Listener<FieldEvent>() {
			@Override
			public void handleEvent(FieldEvent be) {
				if (titleProffesor.getValue()) {
					titleDoctor.setValue(false);
					titleAssociateProffesor.setValue(false);
				}
			}
		});
		
		titleAssociateProffesor = new CheckBox();
		titleAssociateProffesor.setBoxLabel("Associate Prof.");
		titleAssociateProffesor.addListener(Events.OnClick, new Listener<FieldEvent>() {
			@Override
			public void handleEvent(FieldEvent be) {
				if (titleAssociateProffesor.getValue()) {
					titleProffesor.setValue(false);
					titleDoctor.setValue(false);
				}
			}
		});
		
		titleSeniorLecturer = new CheckBox();
		titleSeniorLecturer.setBoxLabel("Senior Lect.");
		titleSeniorLecturer.addListener(Events.OnClick, new Listener<FieldEvent>() {
			@Override
			public void handleEvent(FieldEvent be) {
				if (titleSeniorLecturer.getValue()) {
					titleLecturer.setValue(false);
					titleJuniorLecturer.setValue(false);
					titleTeachingAssistant.setValue(false);
				}
			}
		});
		
		titleLecturer = new CheckBox();
		titleLecturer.setBoxLabel("Lect.");
		titleLecturer.addListener(Events.OnClick, new Listener<FieldEvent>() {
			@Override
			public void handleEvent(FieldEvent be) {
				if (titleLecturer.getValue()) {
					titleSeniorLecturer.setValue(false);
					titleJuniorLecturer.setValue(false);
					titleTeachingAssistant.setValue(false);
				}
			}
		});

		titleJuniorLecturer = new CheckBox();
		titleJuniorLecturer.setBoxLabel("Junior Lect.");
		titleJuniorLecturer.addListener(Events.OnClick, new Listener<FieldEvent>() {
			@Override
			public void handleEvent(FieldEvent be) {
				if (titleJuniorLecturer.getValue()) {
					titleSeniorLecturer.setValue(false);
					titleLecturer.setValue(false);
					titleTeachingAssistant.setValue(false);
				}
			}
		});
		
		titleTeachingAssistant = new CheckBox();
		titleTeachingAssistant.setBoxLabel("Teaching Assist.");
		titleTeachingAssistant.addListener(Events.OnClick, new Listener<FieldEvent>() {
			@Override
			public void handleEvent(FieldEvent be) {
				if (titleTeachingAssistant.getValue()) {
					titleSeniorLecturer.setValue(false);
					titleLecturer.setValue(false);
					titleJuniorLecturer.setValue(false);
				}
			}
		});
		
		if (staffSummary != null && staffSummary.getTitle() != null) {
			if (staffSummary.getTitle().equalsIgnoreCase(
					RattsModelConstants.TITLE_MR)) {
				titleMr.setValue(true);
			} else if (staffSummary.getTitle().equalsIgnoreCase(
					RattsModelConstants.TITLE_MRS)) {
				titleMrs.setValue(true);
			} else if (staffSummary.getTitle().equalsIgnoreCase(
					RattsModelConstants.TITLE_MISS)) {
				titleMiss.setValue(true);
			} else if (staffSummary.getTitle().equalsIgnoreCase(
					RattsModelConstants.TITLE_DOCTOR)) {
				titleDoctor.setValue(true);
			} else if (staffSummary.getTitle().equalsIgnoreCase(
					RattsModelConstants.TITLE_PROFESSOR)) {
				titleProffesor.setValue(true);
			} else if (staffSummary.getTitle().equalsIgnoreCase(
					RattsModelConstants.TITLE_ASSOCIATE_PROFESSOR)) {
				titleAssociateProffesor.setValue(true);
			} else if (staffSummary.getTitle().equalsIgnoreCase(
					RattsModelConstants.TITLE_SENIOR_LECTURER)) {
				titleSeniorLecturer.setValue(true);
			} else if (staffSummary.getTitle().equalsIgnoreCase(
					RattsModelConstants.TITLE_LECTURER)) {
				titleLecturer.setValue(true);
			} else if (staffSummary.getTitle().equalsIgnoreCase(
					RattsModelConstants.TITLE_JUNIOR_LECTURER)) {
				titleJuniorLecturer.setValue(true);
			} else if (staffSummary.getTitle().equalsIgnoreCase(
					RattsModelConstants.TITLE_TEACHING_ASSISTANT)) {
				titleTeachingAssistant.setValue(true);
			}
		} 
		
		staffTitle = new CheckBoxGroup();  
		staffTitle.setFieldLabel("Title");
		staffTitle.add(titleMr);
		staffTitle.add(titleMrs);
		staffTitle.add(titleMiss);
		staffTitle.add(titleDoctor);
		staffTitle.add(titleProffesor);
		staffTitle.add(titleAssociateProffesor);
		staffTitle.add(titleSeniorLecturer);
		staffTitle.add(titleLecturer);
		staffTitle.add(titleJuniorLecturer);
		staffTitle.add(titleTeachingAssistant);
		
		titleContainer.add(staffTitle, formData);

		firstName = new TextField<String>();
		firstName.setFieldLabel("First Name");

		if (staffSummary != null) {
			firstName.setValue(staffSummary.getFirstName());
		}

		left.add(firstName, formData);
		
		lastName = new TextField<String>();
		lastName.setFieldLabel("Last Name");

		if (staffSummary != null) {
			lastName.setValue(staffSummary.getLastName());
		}

		right.add(lastName, formData);
		
		// Gender
		radioMale = new Radio();
		radioMale.setBoxLabel("Male");

		radioFemale = new Radio();
		radioFemale.setBoxLabel("Female");

		gender = new RadioGroup();
		gender.setFieldLabel("Gender");
		gender.add(radioMale);
		gender.add(radioFemale);
		
		if (staffSummary != null) {
			if (staffSummary.getGender().equalsIgnoreCase(RattsModelConstants.GENDER_FEMALE)) {
				radioFemale.setValue(true);
			} else if (staffSummary.getGender().equalsIgnoreCase(RattsModelConstants.GENDER_MALE)) {
				radioMale.setValue(true);
			}
		}
		
		left.add(gender, formData);
		

		phoneNumber = new TextField<String>();
		phoneNumber.setFieldLabel("Phone No.");

		if (staffSummary != null && staffSummary.getPhoneNumber() != null) {
			phoneNumber.setValue(staffSummary.getPhoneNumber());
		}
		right.add(phoneNumber, formData);
		
		emailAddress = new TextField<String>();
		emailAddress.setFieldLabel("Email Adress");

		if (staffSummary != null) {
			emailAddress.setValue(staffSummary.getEmailAddress());
		}
		left.add(emailAddress, formData);

		residentialAddress = new TextField<String>();
		residentialAddress.setFieldLabel("Residential Adress");

		if (staffSummary != null && staffSummary.getResidentialAddress() != null) {
			residentialAddress.setValue(staffSummary.getResidentialAddress());
		}
		right.add(residentialAddress, formData);
		
		if (staffSummary != null) {
			departmentNames = new ComboBox<DepartmentSummary>();
			departmentNames.setFieldLabel("Department");
			departmentNames.setEmptyText("Choose Department");
			departmentNames.setDisplayField("departmentName");
			departmentNames.setTriggerAction(TriggerAction.ALL);
			departmentNames.setStore(getDepartmentStore());
			//departmentNames.setValue(courseSummary.getDepartmentName());
		} else {
			departmentNames = new ComboBox<DepartmentSummary>();
			departmentNames.setFieldLabel("Department");
			departmentNames.setEmptyText("Choose Department");
			departmentNames.setDisplayField("departmentName");
			departmentNames.setTriggerAction(TriggerAction.ALL);
			departmentNames.setStore(getDepartmentStore());
		}
		
		left.add(departmentNames, formData);
		
		dateOfBirth = new DateField();  
		dateOfBirth.setFieldLabel("Date Of Birth");  
		
		if (staffSummary != null) {
			//long dob = Date.parse(staffSummary.getDateOfBirth());
			//dateOfBirth.setValue(new Date(dob));
		} 
		
		right.add(dateOfBirth, formData);
		
		// Employment types
		radioPermanent = new Radio();
		radioPermanent.setBoxLabel("Permanent");

		radioTemporory = new Radio();
		radioTemporory.setBoxLabel("Temporory");

		radioContract = new Radio();
		radioContract.setBoxLabel("Contract");

		if (staffSummary != null && staffSummary.getEmploymentType() != null) {
			if (staffSummary.getEmploymentType().equalsIgnoreCase(
					RattsModelConstants.EMPLOYMENT_TYPE_PERMANENT)) {
				radioPermanent.setValue(true);
			} else if (staffSummary.getEmploymentType().equalsIgnoreCase(
					RattsModelConstants.EMPLOYMENT_TYPE_TEMPORORY)) {
				radioTemporory.setValue(true);
			} else if (staffSummary.getEmploymentType().equalsIgnoreCase(
					RattsModelConstants.EMPLOYMENT_TYPE_CONTRACT)) {
				radioContract.setValue(true);
			}
		} else {
			radioPermanent.setValue(true);
		}

		employmentType = new RadioGroup();
		employmentType.setFieldLabel("Employment Type");
		employmentType.add(radioPermanent);
		employmentType.add(radioTemporory);
		employmentType.add(radioContract);

		left.add(employmentType, formData);

		// Staff Type
		radioFullTime = new Radio();
		radioFullTime.setBoxLabel("Full Time");

		radioPartTime = new Radio();
		radioPartTime.setBoxLabel("Part Time");

		staffType = new RadioGroup();
		staffType.setFieldLabel("Staff Type");
		staffType.add(radioFullTime);
		staffType.add(radioPartTime);
		
		if (staffSummary != null && staffSummary.getStaffType() != null) {
			if (staffSummary.getStaffType().equalsIgnoreCase(
					RattsModelConstants.STAFF_TYPE_FULL_TIMER)) {
				radioFullTime.setValue(true);
			} else if (staffSummary.getStaffType().equalsIgnoreCase(
					RattsModelConstants.STAFF_TYPE_PART_TIMER)) {
				radioPartTime.setValue(true);
			} 
		} 
		
		right.add(staffType, formData);
		
		
		main.add(left, new ColumnData(.5));
		main.add(right, new ColumnData(.5));

		panel.add(titleContainer, new FormData("100%"));
		panel.add(main, new FormData("100%"));

		
		Button buttonSave = new Button(appMessages.save());
		buttonSave.setIcon(IconHelper.createStyle("add"));
		buttonSave.addListener(Events.Select, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				// addNewCourse();
			}
		});
		
		Button buttonCancel = new Button(appMessages.cancel());
		buttonCancel.setIcon(IconHelper.createStyle("cancel"));
		buttonCancel.addListener(Events.Select, new Listener<ButtonEvent>() {
			@Override
			public void handleEvent(ButtonEvent be) {
				closeWindow();
			}
		});

		panel.addButton(buttonSave);
		panel.addButton(buttonCancel);

		FormButtonBinding binding = new FormButtonBinding(panel);
		binding.addButton(buttonSave);

		return panel;
	}
	
	@SuppressWarnings("deprecation")
	private void closeWindow() {
		staffSummary = null;
		ProgressIndicator.showProgressBar();
		window.close();
		ProgressIndicator.hideProgressBar();
	}
}
